logo

Deploying Django with Gunicorn and Nginx

Posted on July 23, 2024

article

Deploying Django with Gunicorn and Nginx

Deploying a Django application can seem daunting at first, but with the right tools and knowledge, it becomes a straightforward process. In this guide, we'll walk through deploying a Django application using Gunicorn as the application server and Nginx as the reverse proxy. This setup is robust, scalable, and suitable for production environments.

Prerequisites

Before we begin, make sure you have:

  • A Django application ready for deployment
  • A server with Ubuntu 20.04 or later installed
  • Python 3.8 or later
  • Basic knowledge of the command line

Step 1: Set Up the Server Environment

First, let's update the system and install the necessary packages:

sudo apt update
sudo apt upgrade -y
sudo apt install python3-pip python3-venv nginx -y

Step 2: Create a Virtual Environment

Navigate to your project directory and create a virtual environment:

cd /path/to/your/project
python3 -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

Install your project dependencies and Gunicorn:

pip install -r requirements.txt
pip install gunicorn

Step 4: Configure Gunicorn

Create a Gunicorn configuration file named gunicorn_config.py in your project directory:

# gunicorn_config.py
bind = "127.0.0.1:8000"
workers = 3
errorlog = "/path/to/your/project/gunicorn-error.log"
accesslog = "/path/to/your/project/gunicorn-access.log"
loglevel = "warning"

Step 5: Create a Systemd Service File

Create a systemd service file to manage Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service

Add the following content (adjust paths as necessary):

[Unit]
Description=Gunicorn daemon for Django project
After=network.target

[Service]
User=your_username
Group=your_group
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/project/venv/bin/gunicorn --config /path/to/your/project/gunicorn_config.py your_project.wsgi:application

[Install]
WantedBy=multi-user.target

Step 6: Start and Enable the Gunicorn Service

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Step 7: Configure Nginx

Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/your_project

Add the following content (adjust the server_name and paths):

server {
listen 80;
server_name your_domain.com www.your_domain.com;

location = /favicon.ico { access_log off; log_not_found off; }

location /static/ {
root /path/to/your/project;
}

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8000;
}
}

Create a symbolic link to enable the site:

sudo ln -s /etc/nginx/sites-available/your_project /etc/nginx/sites-enabled/

Test the Nginx configuration:

sudo nginx -t

If the test is successful, restart Nginx:

sudo systemctl restart nginx

Step 8: Set Up SSL (Optional but Recommended)

For added security, you should set up SSL. You can use Let's Encrypt to get a free SSL certificate:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Follow the prompts to complete the SSL setup.

Conclusion

Congratulations! You've successfully deployed your Django application using Gunicorn and Nginx. This setup provides a robust and scalable environment for your application. Remember to keep your system and packages updated, and regularly check your logs for any issues.

Some key points to remember:

  • Gunicorn acts as the application server, running your Django code.
  • Nginx serves as a reverse proxy, handling static files and forwarding dynamic requests to Gunicorn.
  • The systemd service ensures that Gunicorn starts automatically and restarts if it crashes.

Happy deploying!

Share: